<?php
        namespace Back\Model;
        use Think\Model\RelationModel;
        class SettingModel extends RelationModel {
            //配置关联的属性
            protected  $_link =[
                'options' =>[
              //关系类型，有多个，一对多
                    'mapping_type' => self::HAS_MANY,
                    //关联的模型
                    'class_name'=>'SettingOption',
                    //关联的字段
                    'foreign_key' =>'setting_id',
                ],
            ];
        //开启批量验证

        protected $patchValidate=true;

        //自动验证
        protected $_validate=[
            //自己写规则
        ];
        //填充
        protected $_auto=[
            //自己写填充规则

            //仅仅需要在插入维护
            ['created_at','time',self::MODEL_INSERT,'function'],
            //更新时间，插入和更新是都需要更新
            ['updated_at','time',self::MODEL_BOTH,'function'],
        ];

        //分组配置项

             public function  groupSetting(){
            //获取所有的组
            $group_list=M('SettingGroup')->order('sort_number')->select();
            //获取所有的配置项
            $setting_list=$this
                ->relation(true)
                ->field('s.*, it.type_title')
                ->alias('s')
                 ->join('left join __INPUT_TYPE__ it using(input_type_id)')
                ->order('sort_number')
                ->select();

            $group_setting_list=[];
        //将分组整理成合适的结构
            foreach ($group_list as $group) {
                $group['setting_list'] = [];//初始化，分组内的配置项为空
                //分组ID 作为下标
                $group_setting_list[$group['setting_group_id']] = $group;
            }
                //整合结构
                foreach ($setting_list as $setting){

                    //每个配置项以组ID为下标，标识分组，加入对应的分组的setting_list元素中
                    if('select-multi' ==$setting['type_title']){
                        $setting['value_list']=explode(',',$setting['value']);
                    }
                    $group_setting_list[$setting['setting_group_id']]['setting_list'][]=$setting;
                }
                return $group_setting_list;
            }
        }

